package com.hanson.data_conveying.task.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.hanson.common.constant.Constant;
import com.hanson.data_conveying.task.WendyTask;
import com.hanson.repository.ConveyLogRepository;
import com.hanson.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 传输测试接收接口
 * @classname: ConveyController
 * @author: HansonLee
 * @create: 2024/03/26
 **/
@Slf4j
@RestController
@RequestMapping("/convey")
public class ConveyController {

    @Autowired
    private ConveyLogRepository conveyLogRepository;
    @Autowired
    private WendyTask wendyTask;


    @RequestMapping(value = "/sell_data")
    @ResponseBody
    public String getSellDataConvey(@RequestBody String json){
        System.out.println("-----------server receive data start-----------");
        boolean validate = JSONValidator.from(json).validate();
        if (!validate) return "receive failed,invalid data";
        boolean validArray = JSONObject.isValidArray(json);
        Object parse = validArray? JSONArray.parse(json):JSONObject.toJSON(json);
        if (!Constant.ENVIROMENT_PRO.equals(SpringContextUtil.getActiveProfile())){
            log.info(parse.toString());
        }
        System.out.println("-----------server receive data end-----------");
        return "receive success";
    }

    @GetMapping(value = "/sell_data/save")
    @ResponseBody
    public String saveConvey(String log){
        System.out.println("-----------server receive data start-----------");
        System.out.println("-----------server receive data end-----------");
        return "success";
    }

    @GetMapping(value = "/sell_data/start/1")
    @ResponseBody
    public String startConvey(){
        System.out.println("-----------data convey start-----------");
        wendyTask.convey();
        System.out.println("-----------data convey end-----------");
        return "Convey task is started successfully";
    }
}
